package com.example.diet_sport.controller;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.diet_sport.entity.user;
import com.example.diet_sport.entity.userfood;
import com.example.diet_sport.mapper.userMapper;
import com.sun.org.apache.xml.internal.resolver.helpers.PublicId;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/user")
@RestController
@CrossOrigin
public  class userController{

    @Autowired
    private userMapper userMapper;


    /**
     * 登录
     * @param username
     * @return
     */
    @ResponseBody
    @GetMapping("/login")
    public List<user> login(String username,String password){
        List<user> list =  queryByName(username);
        if (list != null && list.size() > 0) {
            user user = list.get(0);
            if(password.equals(user.getPassword())){
                System.out.println("登陆成功");
                return list;
            }else {System.out.println("密码错误");
                return null;}}
        else {
            System.out.println("用户名错误");
            return null;
        }

    }


    /**
     * 注册用户
     * @param user
     */
    @PostMapping("/add")
    @ResponseBody
    public int add(user user) {
        return userMapper.insert(user);
    }


    /**
     * //根据姓名获得用户
     * @param username
     * @return
     */
    @ResponseBody
    @GetMapping("/userbyname")
    public List<user> queryByName(String username) {
        Map<String, Object> map = new HashMap<>();
        map.put("username",username);
        return userMapper.selectByMap(map);
    }


    /**
     * 获得用户列表
     * @return
     */
    @ResponseBody
    @GetMapping("/userList")
    public List<user> getUserList() {
        return userMapper.selectList(null);
    }

    /**
     * 更新用户信息
     * @param user
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    public int changeUserById(user user) {
        return userMapper.updateById(user);
    }


    /**
     * 删除用户
     * @param name
     * @return
     */
    @ResponseBody
    @PostMapping("/delete")
    public int deleteByName(String name ) {
        int id = queryByName(name).get(0).getId();
        return userMapper.deleteById(id);
    }




    /**
     * //根据id获得用户
     * @param uid
     * @return
     */
    public user queryById(int uid) {
        Map<String, Object> map = new HashMap<>();
        map.put("id",uid);
        return userMapper.selectByMap(map).get(0);
    }

    /**
     * 已打卡天数修改
     */
    @ResponseBody
    @PostMapping("/updateJianchi")
    public int changeJianChiByName(String name,int days) {
        user user = queryByName(name).get(0);
        user.setJianchi(days);
        return userMapper.updateById(user);
    }

    /**
     * 身高查询
     * @param height
     * @return
     */
    @ResponseBody
    @GetMapping("/byh")
    public List<user> querybyh(Double height){
        QueryWrapper<user> wrapper = new QueryWrapper<>();
        wrapper.ge("height",height);
        return userMapper.selectList(wrapper);

    }

}